/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.openbookformat.obffinder.filters;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.openbookformat.tags.CompositeTag;
import org.openbookformat.tags.FnTextTag;
import org.openbookformat.tags.FootnoteTag;
import org.openbookformat.tags.TagUtil;
import org.openbookformat.tags.Text;
import org.openide.util.lookup.ServiceProvider;

/**
 *
 * @author matamh0a
 */
@ServiceProvider(service=Filter.class)
public class FootnotesFormatter extends ReplaceFilter {
    private static String[] fnPatterns = {
      "\\s*[{\\(]?\\s*([\\d+=#@\\$\\!\\*])\\s*[}\\)]?\\s*-?\\s*(.*?)$"
      //"\\s*[{\\Q(\\E]?\\s*([\\d+=\\*\\+#@\\!])\\s*[}\\Q)\\E]?\\s*-?\\s+(.*?)"
    };
    
    public FootnotesFormatter() {
        name = "Footnotes Text Formatter";
        findText = "<footnote>\n.*?\n</footnote>";
        replaceText = "<custom_code>";
        regExp = true;
        ignoreCase = false;
        useVariables = false;
        useIgonreNewLine = true;
        applyToAll = true;
        description = "Format footnotes,\nThis filter does not associate"
                + " footnotes with marks.";
    }
    
    @Override
    public String process(String text) {
        FootnoteTag fnTag = new FootnoteTag();
        Pattern pattern = Pattern.compile(fnPatterns[0]);
        Matcher matcher = null;
        text = text.replaceAll("</?footnote\\s*/?\\s*>", "");
        String[] lines = text.split("\n");
   
        for(int i = 0; i < lines.length; i++) {
            if(lines[i].trim().length() == 0)
                continue;
            
            if(lines[i].matches(fnPatterns[0])) {
                System.out.println("Matched: " + lines[i]);
                matcher = pattern.matcher(lines[i]);
                if(matcher.find())
                    fnTag.addFnTextTag(new FnTextTag(matcher.group(1), 
                            TagUtil.textTagToCompositeTag(matcher.group(2))));                
            } else {
                if(fnTag.isEmpty()) {
                    System.out.println("Empty: " + lines[i]);
                    fnTag.addFnTextTag(new FnTextTag("-1", 
                            TagUtil.textTagToCompositeTag(lines[i])));
                } else {
                    System.out.println("Add: " + lines[i]);
                    fnTag.getFnTextTag(fnTag.size() -1).getText().addTag( 
                            new Text(lines[i]));
                    for(int j = 0; j < fnTag.getFnTextTag(fnTag.size() -1).getText().size(); j++)
                        System.out.println(j + ":" + fnTag.getFnTextTag(fnTag.size() -1).getText().getTag(j));
                }
            }
        }
            
//        if(true)
            return fnTag.toString();
//        
//        String mark = null;
//        String fnText = null;
//        CompositeTag tag = null;
//        while(matcher.find()) {
//            Matcher mat = pattern.matcher(matcher.group());            
//            mark = mat.replaceAll("$1");
//            fnText = mat.replaceAll("$2");
//            tag = new CompositeTag();
//            tag.addTag(new Text(fnText));
//            System.out.println(new FnTextTag(mark, tag).toString());
//            fnTag.addFnTextTag(new FnTextTag(mark, tag));
//        }
//        return fnTag.toString();
    }

}
